package bookshop.beans;

import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {
	private Map<String,BookItem> shoppingCart=new HashMap<>();
	private String bookName;

	public void addOneBook(String name,int price) {
		this.bookName=name;
		BookItem item=shoppingCart.get(name);
		if(item!=null) {
			item.addNum();
		}else {
			item=new BookItem(name,price);
			item.addNum();
			shoppingCart.put(name, item);
		}
			
	}
	//生成一个购物车对象
	public ShoppingCart(String bookName,int price) {
		this.bookName=bookName;
		BookItem item=new BookItem(bookName,price);
		item.addNum();
		shoppingCart.put(bookName, item);
	}
	//得到书的名字
	public String getBookName() {
		return bookName;
	}
	//得到购物车中书总的数量
	public int getTotalNum() {
		int totalNum=0;
		for(BookItem item:shoppingCart.values()) {
			totalNum+=item.getTotalNum();
		}
		return totalNum;
	}
	//得到购物车中总的价格
	public int getTotalMoney() {
		int totalMoney=0;
		for(BookItem item:shoppingCart.values()) {
			totalMoney+=item.getTotalNum()*item.getPrice();
		}
		return totalMoney;
	}
	@Override
	public String toString() {
		return "ShoppingCart [shoppingCart=" + shoppingCart + ", bookName=" + bookName + "]";
	}
	

}
